/**
 *
 */
package com.account.boot.commons.result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.account.boot.commons.exception.APIException;

@SuppressWarnings("rawtypes")
public class ResultDO<T> extends BaseResultDO {
    @SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ResultDO.class);
    /**
     *
     */
    private static final long serialVersionUID = -3434272908589805662L;

    private T data;

    public ResultDO() {
    }

    /**
     * 初始化ResultDO使用该方法
     * @param <T>
     * @return
     */
    public static <T> ResultDO<T> buildResult() {
        return new ResultDO<>(ResultCodeEnum.SUCCESS, true, 0);
    }
    
    public ResultDO(ResultCodeEnum resultCodeEnum, boolean result) {
        setResultCode(resultCodeEnum.getResultCode());
        setErrorMessage(resultCodeEnum.getMessage());
        setSuccess(result);
    }
    
    public ResultDO(APIException apiException, boolean result) {
        setResultCode(apiException.getResultCode());
        setErrorMessage(apiException.getErrorMessage());
        setSuccess(result);
    }

    /**
     * 建议的方法
     * @param resultCodeEnum
     * @param result
     */
    public ResultDO(ResultCodeInterface resultCodeEnum, boolean result) {
        setResultCode(resultCodeEnum.getResultCode());
        setErrorMessage(resultCodeEnum.getMessage());
        setSuccess(result);
    }
    
    /**
     * 复制一份result
     * @param resultDo
     */
    @Deprecated
    public ResultDO(ResultDO resultDo)
    {
    	this.errorMessage = resultDo.getErrorMesssage();
    	this.setResultCode(resultDo.getResultCode());
    	setSuccess(resultDo.isSuccess());
    }

    /**
     * 实例化ResultDO
     * @param resultCodeEnum
     * @param result
     * @param errorType 异常类型 0.业务异常 1.底层系统级异常(db,redis,远程连接有问题,hsf等)
     */
    private ResultDO(ResultCodeInterface resultCodeEnum, boolean result, int errorType) {
        setResultCode(resultCodeEnum.getResultCode());
        setErrorMessage(resultCodeEnum.getMessage());
        setSuccess(result);
        setErrorType(errorType);
    }
    
    @Deprecated
    public ResultDO(T data) {
        this.data = data;
    }


    /**
     * 验证RPC返回体是否正常
     *
     * @param result 返回体
     * @return true:返回错误信息, false:正常返回信息
     */
    public static boolean isError(ResultDO result) {
        return !isSuccess(result);
    }

    /**
     * 验证RPC返回体是否正常
     *
     * @param result 返回体
     * @return true:正常返回信息, false:返回错误信息
     */
    public static boolean isSuccess(ResultDO result) {
        return result.isSuccess() || result.getResultCode() == ResultCodeEnum.SUCCESS.getResultCode();
    }

    @Deprecated
    public static <T> ResultDO<T> getResult() {
        return new ResultDO<T>();
    }

    public T getModule() {
        return data;
    }

    public void setModule(T data) {
        this.data = data;
    }

    /**
     * 拼装返回信息
     * @param resultCodeEnum
     * @param result
     */
	public void setResultInfo(ResultCodeEnum resultCodeEnum, boolean result) {
		setResultCode(resultCodeEnum.getResultCode());
		setErrorMessage(resultCodeEnum.getMessage());
		setSuccess(result);
	}
}
